package com.weishaoying.rws.web;

import com.weishaoying.rws.entity.Book;
import com.weishaoying.rws.entity.Dog;
import com.weishaoying.rws.entity.User;
import com.weishaoying.rws.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author WeiShaoying
 * @date 2018/5/5
 */
@RestController
@RequestMapping(value = "/")
public class TestController {

    Random random = new Random();

    @Autowired
    private UserService userService;

    @GetMapping(value = "/hi")
    public String hello() {
        System.out.println("----hello" + random.nextInt(10000));
        return "Hello-" + random.nextInt(10000);
    }

    // 数据源1-对应的方法

    @GetMapping(value = "/queryAllBook")
    public List<Book> queryBook() {
        return userService.queryBooks();
    }


    @GetMapping(value = "/query/{id}")
    public User query(@PathVariable Long id) {
        System.out.println("id = [" + id + "]");
        return userService.queryUser(id);
    }

    @GetMapping(value = "/queryAllUser")
    public List<User> queryUser() {
        return userService.queryAllUser();
    }


    // 数据源2-对应的方法

    @GetMapping(value = "/queryAllDog")
    public List<Dog> queryDog() {
        return userService.queryDogs();
    }

    @PostMapping(value = "/save")
    public String saveUser(@RequestBody User user) {
        userService.saveUser(user);
        return "OK";
    }

    @PostMapping(value = "/update")
    public String updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return "OK";
    }

    @GetMapping(value = "/delete/{id}")
    public String deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return "OK";
    }

}
